********************************************************************************
** 	TITLE: c5_sag_gen_estimate.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Table V: Alternative Difference-in-IVs Estimates of Recidivism Effects

********************************************************************************
		set sortseed 13

	use "$output_data/estimate.dta", replace
	est clear	
		
********************************************************************************
* Column 1  -- Genesee Diff-in-IV (Baseline)
********************************************************************************
			//stay ignite_0 for Recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec

//IV Regression
		 ivreghdfe return_3m (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			$auxiliary_control ///
			if return_3m>=0, ///
			a($design_control) cluster(inmate)
		
		eststo col1
		lincom ignite_0
				
		local lintemp1 = r(estimate)
		local lin1: display %8.3f `lintemp1'
				
		local linsetemp1 = r(se)
		local linse1: display %8.3f `linsetemp1'
				
		local lin_p1  =r(p)
		
		lincom stay
				
		local lintemp2 = r(estimate)
		local lin2: display %8.3f `lintemp2'
				
		local linsetemp2 = r(se)
		local linse2: display %8.3f `linsetemp2'
				
		local lin_p2  =r(p)
	
	if `lin_p1'<0.01 {
		local star11="***"
	}
	if `lin_p1'>=0.01 & `lin_p1'<0.05 {
		local star11="**"
	}
	if `lin_p1'>=0.05 & `lin_p1'<0.1 {
		local star11="*"
	}
	if `lin_p1'>=0.1 {
		local star11=""
	}
	
	if `lin_p2'<0.01 {
		local star12="***"
	}
	if `lin_p2'>=0.01 & `lin_p2'<0.05 {
		local star12="**"
	}
	if `lin_p2'>=0.05 & `lin_p2'<0.1 {
		local star12="*"
	}
	if `lin_p2'>=0.1 {
		local star12=""
	}
	
	local linc1 = "`lin1'" + "`star11'"
	estadd local combined1 "`linc1'": col1
	local lincse1 = subinstr("(`linse1')"," ","",.)
	estadd local combined_se1 "`lincse1'": col1
	
	local linc2 = "`lin2'" + "`star12'"
	estadd local combined2 "`linc2'": col1
	local lincse2 = subinstr("(`linse2')"," ","",.)
	estadd local combined_se2 "`lincse2'": col1
	
	
********************************************************************************
* Column 2 -- Post-IGNITE Diff-in-IV
********************************************************************************
	preserve
		
		keep if mofd(booking_date)>=mofd(date("09/01/2020","MDY")) & ///
			mofd(booking_date)<mofd(date("06/01/2022","MDY"))
		
		append using "$output_data/sag_post_roa.dta" 


			//stay ignite_0 for Recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec

//IV Regression				
		ivreghdfe return_3m (ignite_0 stay = interact D_remove_any) genesee int_test_* $auxiliary_control ///
			 if return_3m>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control des_int_*)
		eststo col2
		lincom ignite_0
				
		local lintemp1 = r(estimate)
		local lin1: display %8.3f `lintemp1'
				
		local linsetemp1 = r(se)
		local linse1: display %8.3f `linsetemp1'
				
		local lin_p1  =r(p)
		
		lincom stay
				
		local lintemp2 = r(estimate)
		local lin2: display %8.3f `lintemp2'
				
		local linsetemp2 = r(se)
		local linse2: display %8.3f `linsetemp2'
				
		local lin_p2  =r(p)
	
	if `lin_p1'<0.01 {
		local star11="***"
	}
	if `lin_p1'>=0.01 & `lin_p1'<0.05 {
		local star11="**"
	}
	if `lin_p1'>=0.05 & `lin_p1'<0.1 {
		local star11="*"
	}
	if `lin_p1'>=0.1 {
		local star11=""
	}
	
	if `lin_p2'<0.01 {
		local star12="***"
	}
	if `lin_p2'>=0.01 & `lin_p2'<0.05 {
		local star12="**"
	}
	if `lin_p2'>=0.05 & `lin_p2'<0.1 {
		local star12="*"
	}
	if `lin_p2'>=0.1 {
		local star12=""
	}
	
	local linc1 = "`lin1'" + "`star11'"
	estadd local combined1 "`linc1'": col2
	local lincse1 = subinstr("(`linse1')"," ","",.)
	estadd local combined_se1 "`lincse1'": col2
	
	local linc2 = "`lin2'" + "`star12'"
	estadd local combined2 "`linc2'": col2
	local lincse2 = subinstr("(`linse2')"," ","",.)
	estadd local combined_se2 "`lincse2'": col2			

restore 
********************************************************************************
* Column 3 -- Saginaw Diff-in-IV
********************************************************************************
preserve

 use "$output_data/sag_post_roa_all.dta", clear

			//stay ignite_0 for Recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec

//IV Regression				
		ivreghdfe return_3m (ignite_0 stay=D_remove_any interact_proxy_any) D_ignite_proxy ///
			  $auxiliary_control ///
			if return_3m>=0&!genesee, ///
			a($design_control) cluster(inmate)
		
		eststo col3
		lincom ignite_0
				
		local lintemp3 = r(estimate)
		local lin3: display %8.3f `lintemp3'
				
		local linsetemp3 = r(se)
		local linse3: display %8.3f `linsetemp3'
				
		local lin_p3  =r(p)
		
		lincom stay
				
		local lintemp2 = r(estimate)
		local lin2: display %8.3f `lintemp2'
				
		local linsetemp2 = r(se)
		local linse2: display %8.3f `linsetemp2'
				
		local lin_p2  =r(p)
	
	if `lin_p3'<0.01 {
		local star11="***"
	}
	if `lin_p3'>=0.01 & `lin_p3'<0.05 {
		local star11="**"
	}
	if `lin_p3'>=0.05 & `lin_p3'<0.1 {
		local star11="*"
	}
	if `lin_p3'>=0.1 {
		local star11=""
	}
	
	if `lin_p2'<0.01 {
		local star12="***"
	}
	if `lin_p2'>=0.01 & `lin_p2'<0.05 {
		local star12="**"
	}
	if `lin_p2'>=0.05 & `lin_p2'<0.1 {
		local star12="*"
	}
	if `lin_p2'>=0.1 {
		local star12=""
	}
	
	local linc3 = "`lin3'" + "`star13'"
	estadd local combined3 "`linc3'": col3
	local lincse3 = subinstr("(`linse3')"," ","",.)
	estadd local combined_se3 "`lincse3'": col3
	
	local linc2 = "`lin2'" + "`star12'"
	estadd local combined2 "`linc2'": col3
	local lincse2 = subinstr("(`linse2')"," ","",.)
	estadd local combined_se2 "`lincse2'": col3

restore
	
********************************************************************************
* Column 4 -- Double Diff-in-IV
********************************************************************************
	preserve

	append using "$output_data/sag_post_roa_all.dta"
	
			//stay ignite_0 for Recidivism
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
	

//IV Regression				
	ivreghdfe return_3m (genesee#(c.ignite_0 c.stay) = genesee#(interact_proxy_any D_remove_any))  genesee#D_ignite_proxy ///
			int_test_* $auxiliary_control genesee D_ignite_proxy if return_3m>=0, ///
			cluster(inmate) ffirst a($design_control des_int_*)
	eststo col4				
	lincom 1.genesee#c.ignite_0-0.genesee#c.ignite_0
	
	local lintemp1 = r(estimate)
	local lin1: display %8.3f `lintemp1'
	
	local linsetemp1 = r(se)
	local linse1: display %8.3f `linsetemp1'
	
	local lin_p1  =r(p)
	
	
	if `lin_p1'<0.01 {
		local star11="***"
	}
	if `lin_p1'>=0.01 & `lin_p1'<0.05 {
		local star11="**"
	}
	if `lin_p1'>=0.05 & `lin_p1'<0.1 {
		local star11="*"
	}
	if `lin_p1'>=0.1 {
		local star11=""
	}
	
	local linc1 = "`lin1'" + "`star11'"
	estadd local combined1 "`linc1'": col4
	local lincse1 = subinstr("(`linse1')"," ","",.)
	estadd local combined_se1 "`lincse1'": col4
	
	restore
	
********************************************************************************
* Make Tables
********************************************************************************
estout col1 col2 col3 col4   ///
	using "$tables/table5_diff_in_iv_alter.tex", style(tex)  ///
	label nobaselevels collabels(none) mlabels(none) /// 
	drop($auxiliary_control ignite_0 stay D_ignite_proxy int_test_* genesee* 1.genesee#1.D_ignite_proxy 1.genesee#c.stay 1.genesee#c.ignite_0) ///
	cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
	stats(combined1 combined_se1 combined2 combined_se2 combined3 combined_se3 foo1 N, ///
	fmt(%8.3f %8.3f %8.3f %8.3f %8.3f %8.3f 0 %8.0gc) /// 
	labels("Months in IGNITE" " " "Months in Jail" " " "Months in Jail $\times$ Post" " " "\hspace{0.5cm}" "Observations"))	replace